package com.beidu.lottery.call.controller;

import com.beidu.lottery.call.entity.PortalActivityRule;
import com.beidu.lottery.call.entity.dto.OrderBaseDTO;
import com.beidu.lottery.call.entity.query.ActivityAwardRecordsQuery;
import com.beidu.lottery.call.entity.vo.ActivityLotteryResultVO;
import com.beidu.lottery.call.entity.vo.UserAwardRecordDetailVO;
import com.beidu.lottery.call.entity.vo.UserAwardRecordVO;
import com.beidu.lottery.call.result.Res;
import com.beidu.lottery.call.service.portal.ActivityLotteryService;
import com.beidu.lottery.call.service.portal.validator.impl.LotteryValidator;
import com.github.pagehelper.PageInfo;

import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 抽奖活动 前端控制器
 * </p>
 *
 * @author 北渡
 * @since 2023-11-09
 */
@Slf4j
@RestController
@RequestMapping("/activity/lottery")
public class ActivityLotteryController {

    @Resource
    private LotteryValidator lotteryValidator;

    @Resource
    private ActivityLotteryService activityLotteryService;

    /**
     * 抽奖
     *
     * @param id 活动ID
     * @author 北渡
     */
    @PostMapping("/draw/{id}")
    public Res<ActivityLotteryResultVO> lotterySubmit(@PathVariable Integer id) {
        // 抽奖规则校验
        //PortalActivityRule rule = lotteryValidator.validate(id);
        // 抽奖数据预提交
        return Res.success(activityLotteryService.drawPreSubmit(id));
    }

    /**
     * 随机分页查询中奖名单（字符串）
     * <p>
     * 页码数量算法：假设pageSize=10, total=21, 页码数返回2(最大化满足分页数量), total小于分页数量返回1
     * 随机分页算法：total/pageSize做为随机界限值
     *
     * @return 字符串格式：恭喜 {用户昵称} 获得了 {奖品名称}
     * @author 北渡
     */
    @GetMapping("/records/page")
    public Res<List<String>> pageRecords(@Validated ActivityAwardRecordsQuery param) {
        return Res.success(activityLotteryService.pageAwardRecords(param));
    }

    /**
     * 分页查询用户中奖记录
     *
     * @author 北渡
     */
    @GetMapping("/records/user")
    public Res<PageInfo<UserAwardRecordVO>> userRecords(@Validated ActivityAwardRecordsQuery param) {
        return Res.success(activityLotteryService.pageUserRecords(param));
    }

    /**
     * 查询用户中奖记录详情
     *
     * @param number 订单编号
     * @author 北渡
     */
    @GetMapping("/award/{number}")
    public Res<UserAwardRecordDetailVO> recordDetail(@PathVariable String number) {
        return Res.success(activityLotteryService.userRecordDetail(number));
    }

    /**
     * 提交领奖信息（填写收货信息）
     *
     * @author 北渡
     */
    @PostMapping("/receive/submit")
    public Res<Boolean> submitConsigneeInfo(@Validated({OrderBaseDTO.UpdateReceiveInfo.class}) OrderBaseDTO orderBaseDTO) {
        return Res.success(activityLotteryService.submitConsigneeInfo(orderBaseDTO));
    }

}
